#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QThread>
#include "Common/Core.h"
#include "Command/ICommand.h"
#include "Command/Transaction/GetVersion.h"
#include "Command/Transaction/SetUserName.h"
#include "Command/Transaction/GetUserList.h"

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);

	connect(ui->pushButton, SIGNAL(clicked()), SLOT(onBtnSend()));
	connect(ui->btnConnect, SIGNAL(clicked()), SLOT(onBtnConnect()));
	connect(ui->btnDisconnect, SIGNAL(clicked()), SLOT(onBtnDisconnect()));

	_core = new Common::Core();
	connect(_core, SIGNAL(connected()), SLOT(onConnected()));

	_log.setWidget(ui->textEdit);
	_core->setLogManager(&_log);
}

MainWindow::~MainWindow()
{
	_core->closeConnection();
	delete ui;
}

void MainWindow::onBtnConnect()
{
	_core->openConnection("127.0.0.1",666);
}

void MainWindow::onBtnDisconnect()
{
	_core->closeConnection();
}

void MainWindow::onBtnSend()
{
	_core->addCommand(Common::ICommand::SPtr(new Command::GetUserList()));
}

void MainWindow::onConnected()
{
	_core->addCommand(Common::ICommand::SPtr(new Command::GetVersion()));
	_core->addCommand(Common::ICommand::SPtr(new Command::SetUserName(ui->nameEdit->text())));
}
